<template>
  <section id="new">
      <empty v-if="newArr.length === 0" message="还没有最新动态哦！"></empty>
      <ma-scroll v-else :down="true" :up="true" :refresh="refresh" :getMore="getNew"  :showMsg="false" :init="init">
          <dynamic v-for="(obj,index) in newArr" @shield="shield" :index="index" :obj="obj" :key="index"></dynamic>
      </ma-scroll>
      <div class="pubButton" v-if="!isSilent">
          <i class="iconfont icon-bianji" v-tap="{methods: pubDynamic}"></i>
        </div>
  </section>
</template>
<script>
import dynamic from "components/dynamic/dynamic_home";
import getNew from "services/community/getNew";
import empty from "components/empty-page";
import QueryStore from "services/community/QueryStore";
//import shieldService from "services/dynamicService/shield";
	import CreatePostBlacklist from 'services/community/CreatePostBlacklist.js'
//import scroll from "components/scroll/scroll"

export default {
  name: "new",
  components: {
    dynamic,
    empty,
//  scroll
  },
  data() {
    return {
      newArr: [],
      init: false,
      isSilent:false,
      index: 0,
      count: 8,
      flag : true
    };
  },
  methods: {
    getcommunitySilent(){
      QueryStore.bind(this)({

      },resp=>{
        console.log(resp.shop.communitySilent)
        this.isSilent = resp.shop.communitySilent === '1'? true : false
      })
    },
    refresh(callback){
      let vm = this;
      vm.index = 0;
         return new Promise(function(resolve, reject) {
        getNew.bind(vm)(
          {
            index: vm.index + "",
            count: vm.count + ""
          },
          res => {
            vm.newArr=res;
            vm.index += vm.count;
               vm.init = true;
               resolve(callback);
          }
        );
         });
    },
    getNew(callback) {
      if(this.flag){
        this.flag = false
        let vm = this;
         return new Promise(function(resolve, reject) {
        getNew.bind(vm)(
          {
            index: vm.index + "",
            count: vm.count + ""
          },
          res => {
            vm.newArr.push(...res);
            vm.index += vm.count;
               vm.init = true;
               vm.flag = true
               resolve(callback);
          }
        );
      });
      }
      
    },
    //屏蔽信息
    shield(params) {
      if(this.$.userId){
        	console.log(params)
          CreatePostBlacklist.bind(this)(
            {
              id: params.id
            },
            res => {
              this.newArr.splice(params.index, 1);
              this.$.toast("屏蔽信息成功");
            },
            err => {}
          );
      }else{
        this.$.goLogin({
            name: "login"
          });
      }
    
    },
    pubDynamic(){
      if(this.$.userId){
        this.$router.push({
            name:'pub_dynamic'
        })
      }else{
        this.$.goLogin({
            name: "login"
          });
      }
        
    }
  },
  mounted() {
      let vm =this
      this.getcommunitySilent()
    getNew.bind(this)(
          {
            index: vm.index + "",
            count: vm.count + ""
          },
          res => {
            console.log(res);
            vm.newArr.push(...res);
            vm.index += vm.count;
            vm.init = true;
            console.log(vm.newArr);
          }
        );
  }
};
</script>
<style lang="scss">
#new {
  .scroller{
    width: 100%;
  }
    #wrapper{
        top: 100px;
    }
    .pubButton{
        position: fixed;
        bottom: 5vh;
        right: 20px;
        z-index: 100;
        text-align: center;
            width: 40px;
            height: 40px;
            background: #f98700;
            border-radius: 50%;
        i{
            line-height: 40px;
            color: #fff;
            font-size: large;
            padding-left: 4px;
        }
    }
}
</style>


